package com.draw.server.gui.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import org.hibernate.Query;
import org.hibernate.Session;

import com.draw.core.Word;
import com.draw.server.bdd_manager.WordManager;
import com.draw.server.event.SelectionChange;
import com.draw.server.gui.model.DataTableModel;
import com.draw.server.hibernate.HibernateUtil;
import com.draw.server.service.WordService;

public class WordPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 3578365842799697676L;
	private static Dimension fieldDimension = new Dimension(100, 30);

	private static WordPanel instance = new WordPanel();
	private SpinnerModel sm = new SpinnerNumberModel(1, 1, 3, 1);
	private JTextField wordField;
	private JSpinner difficultyField;
	private JButton save;
	private JButton update;
	private JButton cancel;
	private JButton delete;
	private JTable wordTable;
	private DataTableModel dataTableModel;
	
	private String[] columnNames = {"Mot",
									"Difficulté"};

	public static WordPanel getInstance() {
		return instance;
	}
	
	public Object[][] getAllWords() {	
		WordService ws = new WordService();
		List<Word> wordList = ws.getAll();
		
		Object[][] data = new Object[wordList.size()][2];

		int i=0;
		for(Word word : wordList) {
			data[i][0] = word.getWord();
			data[i][1] = String.valueOf(word.getLevel());			
			i++;
		}

		return data;
	}

	public WordPanel() {
		Object[][] data = this.getAllWords();
		
		this.setLayout(new BorderLayout());
		
		JPanel pan = new JPanel(new FlowLayout());
		this.add(new JLabel("Mot"));
		this.wordField = new JTextField();
		this.wordField.setPreferredSize(fieldDimension);
		pan.add(this.wordField);
		
		this.add(new JLabel("Difficulté"));
		this.difficultyField = new JSpinner(sm);
		this.difficultyField.setPreferredSize(fieldDimension);
		pan.add(this.difficultyField);

		this.save = new JButton("Sauvegarder");
		this.save.addActionListener(this);
		pan.add(this.save);
		
		this.update = new JButton("Modifier");
		this.update.addActionListener(this);
		pan.add(this.update);
		
		this.delete = new JButton("Delete");
		this.delete.addActionListener(this);
		pan.add(this.delete);
		
		this.cancel = new JButton("Annuler");
		this.cancel.addActionListener(this);
		pan.add(this.cancel);
		
		this.dataTableModel = new DataTableModel();
		dataTableModel.setHeader(this.columnNames);
		dataTableModel.setData(data);
		
		this.wordTable = new JTable(dataTableModel);
		this.wordTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		
		this.wordTable.setFillsViewportHeight(true);
		this.wordTable.getSelectionModel().addListSelectionListener(new SelectionChange(this.wordTable, this.wordField, this.difficultyField));
		
		JScrollPane scrollPane = new JScrollPane(this.wordTable);
		this.add(scrollPane);
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(pan,BorderLayout.SOUTH);
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource().equals(this.save) ) {
			Word word = new Word();
			word.setLevel(Integer.parseInt(this.difficultyField.getValue().toString()));
			word.setWord(this.wordField.getText().toUpperCase());
			
			WordManager.createWord(word);
			
			this.dataTableModel.setData(this.getAllWords());
			
			
		} else if ( e.getSource().equals(this.update) && this.wordTable.getSelectedRow() > -1) {
			
			Session session = HibernateUtil.getSession();
			Query query = session.createQuery("from com.draw.core.Word where word = :word");
			query.setString("word", (String) this.getAllWords()[this.wordTable.getSelectedRow()][0]);
			
			Word word = (Word) query.list().get(0);
			word.setLevel(Integer.parseInt(this.difficultyField.getValue().toString()));
			word.setWord(this.wordField.getText().toUpperCase());
			
			WordManager.updateWord(word);
			
			this.dataTableModel.setData(this.getAllWords());
			
		} else if ( e.getSource().equals(this.delete) && this.wordTable.getSelectedRow() > -1 ) {
			Session session = HibernateUtil.getSession();
			Query query = session.createQuery("from com.draw.core.Word where word = :word");
			query.setString("word", (String) this.getAllWords()[this.wordTable.getSelectedRow()][0]);
			
			Word word = (Word) query.list().get(0);
			
			WordManager.deleteWord(word);
			
			this.dataTableModel.setData(this.getAllWords());
		} else {
			this.wordField.setText("");
			this.difficultyField.setValue(1);
		}
	}
}
